{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python期中总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### if语句与range循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_base week1={\n",
    "    \"if 语句\":{\n",
    "        '理解意义，100（90）',\n",
    "        '具体操作,100(80)'\n",
    "if True:\n",
    "    print (\"True\")\n",
    "else:\n",
    "    print (\"False\")\n",
    " \n",
    "      \n",
    "                   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_base week1={\n",
    "    \"for循环与range语句\":{\n",
    "        '理解意义，100（90）',\n",
    "        '具体操作,100(80)'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for <variable> in <sequence>:\n",
    "    <statements>\n",
    "else:\n",
    "    <statements>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##九九乘法表\n",
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(j,\"*\",i,\"=\",i * j,\"\\t\",end=\"\")\n",
    "        print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#range函数  range(start, stop[, step])\n",
    "range(0, 30, 5)  # 步长为 5\n",
    "[0, 5, 10, 15, 20, 25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3,4,5]\n",
    "for i in range(len(a)):\n",
    "     print(a[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "从1加到10\n",
    "        sum=0\n",
    "        for a in range(1,11):\n",
    "        sum+=a\n",
    "        print(sum)\n",
    "        55\n",
    "        for a in range(5)\n",
    "        print('1'\n",
    "        1 1 1 1 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "购物车项目\n",
    "product_list=[('Ovaltine',4),\n",
    "              ('Vita',3),\n",
    "              ('lemon tea',3),\n",
    "              ('book',100),\n",
    "              (\"AG\",600),\n",
    "              ('Computer',6000),\n",
    "              ('glass',300),\n",
    "              ('phone',6000)]\n",
    "shopping_car=[]\n",
    "balance =input('请输入支付宝余额')\n",
    "while True:\n",
    "    balance=int(balance)\n",
    "    for item in product_list:\n",
    "        print(product_list.index(item),item)\n",
    "    choice=input('请输入产品编号')\n",
    "    if choice.isdigit():\n",
    "        choice=int(choice)\n",
    "        if choice >=0 and choice<8:\n",
    "            if balance>=product_list[choice][1]:\n",
    "                balance=balance-product_list[choice][1]\n",
    "                shopping_car.append(product_list[choice][0])\n",
    "                print(\"你把商品%s添加到购物车支付宝余额是%s\"%(product_list[choice][0],balance))\n",
    "                print(shopping_car)\n",
    "                print('默认继续购买,若不想购买请按q键退出')\n",
    "            else:\n",
    "                print('你的余额不足,请重新选择商品')\n",
    "                print('或者请按Q退出')\n",
    "        else:\n",
    "            print('商品里没有这个列表，请重新选择一个商品')\n",
    "    elif choice =='q':\n",
    "        print('-----购物车清单----')\n",
    "        for item in shopping_car:\n",
    "            print(item)\n",
    "        print('你的支付宝余额为%s元'%balance)\n",
    "        print('以购买{}'.format(shopping_car))\n",
    "        break\n",
    "    else:\n",
    "        print('请按Q退出程序')                                       "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### random函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_base week1={\n",
    "    \"for循环与range语句\":{\n",
    "        '理解意义，100（80）',\n",
    "        '具体操作,100(70)'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "print( random.randint(1,10) )        # 产生 1 到 10 的一个整数型随机数  \n",
    "print( random.random() )             # 产生 0 到 1 之间的随机浮点数\n",
    "print( random.uniform(1.1,5.4) )     # 产生  1.1 到 5.4 之间的随机浮点数，区间可以不是整数\n",
    "print( random.choice('tomorrow') )   # 从序列中随机选取一个元素\n",
    "print( random.randrange(1,100,2) )   # 生成从1到100的间隔为2的随机整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "input(\"请输入你的名字\")\n",
    "import random\n",
    "a=random.randint(1,2)\n",
    "if a ==1:\n",
    "    print(\"祝你平安快乐\")\n",
    "if a ==2:\n",
    "    print(\"祝你前程似锦\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "var1[0]:  H\n",
      "var2[1:5]:  unoo\n"
     ]
    }
   ],
   "source": [
    "#截取字符串\n",
    "var1 = 'Hello World!'\n",
    "var2 = \"Runoob\"\n",
    " \n",
    "print (\"var1[0]: \", var1[0])\n",
    "print (\"var2[1:5]: \", var2[1:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新 :  Hello Yang!\n"
     ]
    }
   ],
   "source": [
    "#更新字符串\n",
    "var1 = 'Hello World!'\n",
    " \n",
    "print (\"更新 : \", var1[:6] + 'Yang!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello yang\n",
      "Hello-yang\n"
     ]
    }
   ],
   "source": [
    "#字符串的拼接 join方法\n",
    "str_list = ['Hello', 'yang']\n",
    "str_join1 = ' '.join(str_list)\n",
    "str_join2 = '-'.join(str_list)\n",
    "print(str_join1)  \n",
    "print(str_join2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#字符串的分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1', '2', '3']\n"
     ]
    }
   ],
   "source": [
    "a='1,2,3'\n",
    "b=a.split(',')\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['php中文网', '>>>', 'c.biancheng.net']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " >>> str = \"php中文网 >>> c.biancheng.net\"\n",
    ">>> str\n",
    "'php中文网 >>> c.biancheng.net'\n",
    ">>> list1 = str.split() #采用默认分隔符进行分割\n",
    ">>> list1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的切片\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "num = [\"a\", 1, 1, 1, 2, 3, 4, 5, 6, 7, 8]\n",
    "# count 查找元素出现多少次 \n",
    "print(num.count(1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "a='1,2,3,1'\n",
    "print(a.count('1'))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张冰丽 潘丽璇\n"
     ]
    }
   ],
   "source": [
    "names = [\"林从龙\",\"张冰丽\",\"潘丽璇\",\"黄炜\",\"蔡明丽\"]\n",
    "print(names[1],names[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['潘丽璇', '黄炜', '蔡明丽']\n"
     ]
    }
   ],
   "source": [
    "names = [\"林从龙\",\"张冰丽\",\"潘丽璇\",\"黄炜\",\"蔡明丽\"]\n",
    "print(names[-3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['林从龙', '张冰丽', '潘丽璇', '黄炜', '蔡明丽', '汪铭宇']\n"
     ]
    }
   ],
   "source": [
    "#增加在列表末尾\n",
    "names.append(\"汪铭宇\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['林从龙', '张冰丽', '李婧雯', '潘丽璇', '黄炜', '蔡明丽', '汪铭宇']\n"
     ]
    }
   ],
   "source": [
    "#在某个位置插入 \n",
    "names.insert(2,\"李婧雯\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "李婧雯\n",
      "['林从龙', '张冰丽', '潘丽璇', '黄炜', '蔡明丽', '汪铭宇']\n"
     ]
    }
   ],
   "source": [
    "pop_names = names.pop(2) # 被删除的内容\n",
    "print(pop_names)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['林从龙', '张冰丽', '潘丽璇', '黄炜', '蔡明丽']\n"
     ]
    }
   ],
   "source": [
    "# remove 可以指定内容\n",
    "names.remove(\"汪铭宇\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['陈颖', '张冰丽', '潘丽璇', '黄炜', '蔡明丽']\n"
     ]
    }
   ],
   "source": [
    "# 3、修改 我需要清楚知道他的位置\n",
    "names[0] = \"陈颖\"\n",
    "print(names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "['陈颖', '张冰丽', '潘丽璇', '黄炜', '蔡明丽']\n"
     ]
    }
   ],
   "source": [
    "# 4、查 ------>改或者其他操作\n",
    "# 适合我不知道位置的修改方法：\n",
    "# 查，有没有？\n",
    "_name = \"张冰丽\"\n",
    "if _name in names:\n",
    "    print(names.index(_name)) # index 反馈回在不在，在的位置 1\n",
    "    # names[1] = \"黄炜\" 改\n",
    "    # names.pop(1) 删\n",
    "# 如果存在我们的列表中。我们可以做一些操作，例如删除、改\n",
    "else:\n",
    "    print(\"%s 不在我们的列表中\" %(_name))\n",
    "\n",
    "print(names)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict['Name']: \n"
     ]
    }
   ],
   "source": [
    "#字典的创建\n",
    "dict = {'Name': 'Yang', 'Age': 28, 'Class': 'First'}\n",
    "print (\"dict['Name']: \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict['Age']:  8\n",
      "dict['School']:  中山大学南方学院\n"
     ]
    }
   ],
   "source": [
    "#字典的更新、添加\n",
    "dict = {'Name': 'Yang', 'Age': 28, 'Class': 'First'} \n",
    "dict['Age'] = 8               # 更新 Age\n",
    "dict['School'] = \"中山大学南方学院\"  # 添加信息\n",
    "print (\"dict['Age']: \", dict['Age'])\n",
    "print (\"dict['School']: \", dict['School'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Age': 7, 'Class': 'First'}\n"
     ]
    }
   ],
   "source": [
    "dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} \n",
    "del dict['Name'] # 删除键 'Name'\n",
    "print (dict)\n",
    " \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "#清空字典\n",
    "dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}\n",
    "dict.clear()\n",
    "print (dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "#删除字典\n",
    "dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} \n",
    "del dict \n",
    "print (dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典的内置函数\n",
    "#计算字典个数\n",
    "dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}\n",
    "len(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"{'Name': 'Runoob', 'Age': 7, 'Class': 'First'}\""
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#输出字典\n",
    "dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}\n",
    "str(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#返回输入的变量类型\n",
    "dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}\n",
    "type(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字典长度 : 2\n",
      "字典删除后长度 : 0\n"
     ]
    }
   ],
   "source": [
    "#字典的内置方法\n",
    "#clear Python 字典 clear() 函数用于删除字典内所有元素\n",
    "dict = {'Name': 'Zara', 'Age': 7}\n",
    "\n",
    "print (\"字典长度 : %d\" %  len(dict))\n",
    "dict.clear()\n",
    "print (\"字典删除后长度 : %d\" %  len(dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "score={'语文':95,'数学':90}\n",
    "score.clear()\n",
    "print(score) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新复制的字典为 :  {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}\n"
     ]
    }
   ],
   "source": [
    "#copy Python 字典 copy() 函数返回一个字典的浅复制\n",
    "dict1 = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}\n",
    "dict2 = dict1.copy()\n",
    "print (\"新复制的字典为 : \",dict2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新的字典为 : {'name': None, 'age': None, 'sex': None}\n",
      "新的字典为 : {'name': 10, 'age': 10, 'sex': 10}\n"
     ]
    }
   ],
   "source": [
    "#form mankeys  ython 字典 fromkeys() 函数用于创建一个新字典，以序列 seq 中元素做字典的键，value 为字典所有键对应的初始值\n",
    "seq = ('name', 'age', 'sex')\n",
    " \n",
    "dict = dict.fromkeys(seq)\n",
    "print (\"新的字典为 : %s\" %  str(dict))\n",
    " \n",
    "dict = dict.fromkeys(seq, 10)\n",
    "print (\"新的字典为 : %s\" %  str(dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age 键的值为 : 7\n",
      "Sex 键的值为 : None\n",
      "新字典为： {'Name': 'Runoob', 'Age': 7, 'Sex': None}\n"
     ]
    }
   ],
   "source": [
    "#setdefault  Python 字典 setdefault() 方法和 get()方法 类似, 如果键不已经存在于字典中，将会添加键并将值设为默认值\n",
    "dict = {'Name': 'Runoob', 'Age': 7}\n",
    " \n",
    "print (\"Age 键的值为 : %s\" %  dict.setdefault('Age', None))\n",
    "print (\"Sex 键的值为 : %s\" %  dict.setdefault('Sex', None))\n",
    "print (\"新字典为：\", dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('url', 'www.runoob.com')\n",
      "{'name': '菜鸟教程', 'alexa': 10000}\n"
     ]
    }
   ],
   "source": [
    "#pop  Python 字典 pop() 方法删除字典给定键 key 及对应的值，返回值为被删除的值。key 值必须给出。 否则，返回 default 值\n",
    "site= {'name': '菜鸟教程', 'alexa': 10000, 'url': 'www.runoob.com'}\n",
    "pop_obj=site.popitem()\n",
    "print(pop_obj)\n",
    "print(site)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('url', 'www.runoob.com')\n",
      "{'name': '菜鸟教程', 'alexa': 10000}\n"
     ]
    }
   ],
   "source": [
    "#popitem  ython 字典 popitem() 方法随机返回并删除字典中的最后一对键和值。\n",
    "#如果字典已经为空，却调用了此方法，就报出KeyError异常。\n",
    "site= {'name': '菜鸟教程', 'alexa': 10000, 'url': 'www.runoob.com'}\n",
    "pop_obj=site.popitem()\n",
    "print(pop_obj)   \n",
    "print(site)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新字典 dict :  {'Name': 'Runoob', 'Age': 7, 'Sex': 'female'}\n"
     ]
    }
   ],
   "source": [
    "#update Python 字典 update() 函数把字典参数 dict2 的 key/value(键/值) 对更新到字典 dict 里\n",
    "dict = {'Name': 'Runoob', 'Age': 7}\n",
    "dict2 = {'Sex': 'female' }\n",
    " \n",
    "dict.update(dict2)\n",
    "print (\"更新字典 dict : \", dict)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age 值为 : 27\n",
      "Sex 值为 : NA\n"
     ]
    }
   ],
   "source": [
    "#get Python 字典 get() 函数返回指定键的值\n",
    "dict = {'Name': 'Runoob', 'Age': 27}\n",
    "\n",
    "print (\"Age 值为 : %s\" %  dict.get('Age'))\n",
    "print (\"Sex 值为 : %s\" %  dict.get('Sex', \"NA\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age 值为 : 27\n",
      "Sex 值为 : NA\n"
     ]
    }
   ],
   "source": [
    "#value Python 字典 values() 方法返回一个迭代器，可以使用 list() 来转换为列表，列表为字典中的所有值\n",
    "dict = {'Name': 'Runoob', 'Age': 27}\n",
    "\n",
    "print (\"Age 值为 : %s\" %  dict.get('Age'))\n",
    "print (\"Sex 值为 : %s\" %  dict.get('Sex', \"NA\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "#集合（set）是一个无序的不重复元素序列。\n",
    "#可以使用大括号 { } 或者 set() 函数创建集合，注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{(1, 2), 1, 2, 3}\n"
     ]
    }
   ],
   "source": [
    "#增加元素\n",
    "set = {1, 2, 3}\n",
    "set.add((1, 2))\n",
    "print(set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set()\n"
     ]
    }
   ],
   "source": [
    "#移除元素\n",
    "fruits = {\"apple\", \"banana\", \"cherry\"}\n",
    "fruits.clear()\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry'}\n"
     ]
    }
   ],
   "source": [
    "#移除指定元素\n",
    "fruits = {\"apple\", \"banana\", \"cherry\"}\n",
    "fruits.remove(\"banana\")  \n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'banana', 'cherry', 'runoob', 'google'}\n"
     ]
    }
   ],
   "source": [
    "#合并两个集合，重复元素只会出现一次：\n",
    "x = {\"apple\", \"banana\", \"cherry\"}\n",
    "y = {\"google\", \"runoob\", \"apple\"}\n",
    " \n",
    "x.update(y) \n",
    " \n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry'}\n"
     ]
    }
   ],
   "source": [
    "#discard() 方法用于移除指定的集合元素。\n",
    "#该方法不同于 remove() 方法，因为 remove() 方法在移除一个不存在的元素时会发生错误，而 discard() 方法不会\n",
    "fruits = {\"apple\", \"banana\", \"cherry\"} \n",
    "fruits.discard(\"banana\")  \n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple'}\n"
     ]
    }
   ],
   "source": [
    "#intersection() 方法用于返回两个或更多集合中都包含的元素，即交集\n",
    "x = {\"apple\", \"banana\", \"cherry\"}\n",
    "y = {\"google\", \"runoob\", \"apple\"}\n",
    " \n",
    "z = x.intersection(y) \n",
    " \n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n"
     ]
    }
   ],
   "source": [
    "#pop() 方法用于随机移除一个\n",
    "fruits = {\"apple\", \"banana\", \"cherry\"}\n",
    " \n",
    "x = fruits.pop() \n",
    " \n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'banana', 'cherry', 'runoob', 'google'}\n"
     ]
    }
   ],
   "source": [
    "#union() 方法返回两个集合的并集，即包含了所有集合的元素，重复的元素只会出现一次\n",
    "x = {\"apple\", \"banana\", \"cherry\"}\n",
    "y = {\"google\", \"runoob\", \"apple\"}\n",
    " \n",
    "z = x.union(y) \n",
    " \n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'cherry', 'runoob', 'google'}\n"
     ]
    }
   ],
   "source": [
    "#symmetric_difference() 方法返回两个集合中不重复的元素集合，即会移除两个集合中都存在的元素\n",
    "x = {\"apple\", \"banana\", \"cherry\"}\n",
    "y = {\"google\", \"runoob\", \"apple\"}\n",
    " \n",
    "z = x.symmetric_difference(y) \n",
    " \n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数的学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "  '函数的初步学习':{\n",
    "        '函数关键字def return':['理解意义，100（90）','具体操作,100(80)'],\n",
    "        '函数两种添加注释方法':['理解意义，100（90）','具体操作,100(90)'],\n",
    "        # 方法一’‘’str‘’‘  方法二 #\n",
    "        '函数的*args用法':['理解意义，100（80）','具体操作,100(80)'],\n",
    "        # 传入多个参数，转化成元组\n",
    "        '函数的**kwargs用法':['理解意义，100（80）','具体操作,100(80)'],\n",
    "        # 把关键字参数，转化成字典\n",
    "        '函数的调用 import':['理解意义，100（100）','具体操作,100(90)'],\n",
    "        '函数的as用法':['理解意义，100（100）','具体操作,100(90)'],\n",
    "        '从模块中导入函数':['理解意义，100（90）','具体操作,100(80)'],"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
